package com.xzavier.yunlian_interview.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzavier.yunlian_interview.entity.QuestionItem;
import com.xzavier.yunlian_interview.entity.StudyDuration;
import com.xzavier.yunlian_interview.mapper.QuestionItemMapper;
import com.xzavier.yunlian_interview.service.ex.StudyDurationException;
import com.xzavier.yunlian_interview.view.StudyDuration_RequestParams;
import com.xzavier.yunlian_interview.entity.User;
import com.xzavier.yunlian_interview.mapper.StudyDurationMapper;
import com.xzavier.yunlian_interview.mapper.UserMapper;
import com.xzavier.yunlian_interview.service.StudyDurationService;
import com.xzavier.yunlian_interview.view.StudyHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudyDurationServiceImpl implements StudyDurationService {
    @Autowired
    StudyDurationMapper studyDurationMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    QuestionItemMapper questionItemMapper;

    @Override
    public void recordStudyDuration(String userId, StudyDuration_RequestParams[] arr) {
        int sum = 0;
        for (StudyDuration_RequestParams params : arr) {
            StudyDuration studyDuration;
            QueryWrapper<StudyDuration> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userId", userId);
            queryWrapper.eq("questionId", params.getQuestionId());
            studyDuration = studyDurationMapper.selectOne(queryWrapper);
            if (studyDuration == null) {
                studyDuration = new StudyDuration();
                studyDuration.setUserId(userId);
                studyDuration.setQuestionId(params.getQuestionId());
                studyDuration.setStudyDate(new Date(params.getStartTime()));
                studyDuration.setDuration((int) (params.getEndTime() - params.getStartTime()));
                studyDuration.setCreatedAt(new Date());
                studyDurationMapper.insert(studyDuration);
            } else {
                studyDuration.setDuration(studyDuration.getDuration() + (int) (params.getEndTime() - params.getStartTime()));
                studyDuration.setCreatedAt(new Date(params.getStartTime()));
                studyDurationMapper.updateById(studyDuration);
            }


            sum += studyDuration.getDuration();
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        User user = userMapper.selectOne(queryWrapper);
        user.setTotalTime(user.getTotalTime() + sum);
        userMapper.updateById(user);

    }

    @Override
    public StudyHistory getStudyDuration(String userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new StudyDurationException("用户不存在");
        }
        StudyHistory studyHistory = new StudyHistory();
        QueryWrapper<StudyDuration> studyDurationQueryWrapper = new QueryWrapper<>();
        studyDurationQueryWrapper.eq("userId", userId);
        List<StudyDuration> studyDurations = studyDurationMapper.selectList(studyDurationQueryWrapper);
        studyHistory.setStudyDurations(studyDurations);
        QueryWrapper<QuestionItem> questionItemQueryWrapper = new QueryWrapper<>();
        questionItemQueryWrapper.in("id", studyDurations.stream().map(StudyDuration::getQuestionId).collect(Collectors.toList()));
        List<QuestionItem> questionItems = questionItemMapper.selectList(questionItemQueryWrapper);
        studyHistory.setQuestionItems(questionItems);
        return studyHistory;
    }
}
